package com.game.server.message.proto.execute;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyUnionChangesQuotaDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYfindUnionClubeduLogReq;
import com.game.server.message.proto.out.XYclubPlayerxinyuLogStruct;
import com.game.server.message.proto.out.XYfindUnionClubeduLogRsp;
import com.game.server.message.proto.out.XYunionClubeduLogStruct;
import com.game.server.util.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.stream.Collectors;

public class XYfindUnionClubeduLogReq extends AbstractXYfindUnionClubeduLogReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null && client.checkClick(msgcode)) {
            int limit = 20;
            XYfindUnionClubeduLogRsp rsp = new XYfindUnionClubeduLogRsp();
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
            XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(this.unionid);
            if (unionInfo != null && clubInfo != null) {
                String join = String.join(",", unionInfo.getClubids().stream().map(String::valueOf).collect(Collectors.toList()));
                String[] strs = join.split(",");
                if (Util.useSetArr(strs, clubInfo.getId() + "")) {
//                    XyClubMemberInfo memberInfo = new XyClubMemberInfo();
//                    if(unionInfo.getCreator_clubid() == this.clubid){
//
//                    }
//                    XyClubInfo creatorClubInfo = DataManager.getInstance().getMapClub().get(unionInfo.getCreator_clubid());
//                    memberInfo = creatorClubInfo.getPlayer().get(client.getUserId());

//                    if (memberInfo != null && (memberInfo.getIsManager() == 1 || memberInfo.getIsManager() == 2)) {
                        JSONObject json = XyUnionChangesQuotaDao.findPage(limit, this.page, this.unionid, this.clubid, this.type);
                        rsp.setAllpage(json.getInt("allpage"));
                        rsp.setAllcount(json.getInt("allcount"));
                        rsp.setUnionedu(clubInfo.getUnionedu());
                        rsp.setPage(this.page);
                        JSONArray array = json.getJSONArray("array");
                        XYunionClubeduLogStruct[] unionClubeduLogStructs = new XYunionClubeduLogStruct[array.size()];
                        int index = 0;
                        for (Object ent : array) {
                            JSONObject object = JSONObject.fromObject(ent);
                            XYunionClubeduLogStruct unionClubeduLogStruct = new XYunionClubeduLogStruct();
                            unionClubeduLogStruct.setOptime(object.getString("createdate"));
                            unionClubeduLogStruct.setType(object.getInt("type"));
                            unionClubeduLogStruct.setQuota(object.getInt("quota"));
                            unionClubeduLogStruct.setMeId(object.getInt("meUid"));
                            unionClubeduLogStruct.setResidue(object.getInt("residue"));
                            unionClubeduLogStruct.setRoomkey(object.getString("roomkey"));
                            unionClubeduLogStructs[index] = unionClubeduLogStruct;
                            index++;
                        }
                        rsp.setUnionid(unionInfo.getId());
                        rsp.setClubid(clubInfo.getId());
                        rsp.setName(clubInfo.getName());
                        rsp.setHead(clubInfo.getHeadurl());
                        rsp.setQuotaPlayerCount(clubInfo.getNumberOfPeopleWithQuota());
                        rsp.setAllPlayerCount(clubInfo.getPlayerCount());
                        rsp.setUnionClubeduLogs(unionClubeduLogStructs);
//                    }
                }
            }
            client.Send(rsp);
        }
    }

}
